<?php ob_start(); header('Cache-Control: no-store, no-cache, must-revalidate');

	$username = "root";
	$password = "MYSQL";
	$hostname = "localhost";
	$database = "bike_schema";
	$flagRoute = '0';



	// Opens a connection to a MySQL server
	$connection=mysql_connect ($hostname, $username, $password);
	if (!$connection) {
		die('Not connected : ' . mysql_error());
	}

	// Set the active MySQL database
	$db_selected = mysql_select_db($database, $connection);
	if (!$db_selected) {
		die ('Can not use db : ' . mysql_error());
	}


	// if the user tries to save data
	if($_REQUEST['command']=='save')
	{



	if($_POST['flagRoute']=='1'){
			$data = $_REQUEST['mapdata'];
			$description = $_REQUEST['description'];
			$routeType = $_REQUEST['routeType'];
			$flagRoute = $_REQUEST['flagRoute'];
		}


//if also saving GPX file

	if($flagRoute=='0')
	{
		// Where the file is going to be placed, needs to be in the same
		//location with this php file
		$target_path = "./";

		/* Add the original filename to our target path.
		Result is "uploads/filename.extension" */
		$target_path = $target_path .
		basename( $_FILES['uploadedfile']['name']);
		//var_dump($_FILES);
		//var_dump($_POST);
		if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "$target_path"))
		{
			   echo "The file " .  basename( $_FILES['uploadedfile']['name']) . " has been uploaded";
		}
		else
		{
			   echo "There was an error uploading the file, please try again! oops";
		}

		/*
		uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with.
		$_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
		$_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name
		*/

		$data = $_FILES['uploadedfile']['name'];
		$description = "GPX file";
		$routeType = "Casual";
		//$description = $_POST['description'];
		//$routeType = $_POST['routeTypeSel'];
		//TODO: for some reason these form values aren not available when submitting a file.


	} //end upload GPX file






		$query = "INSERT INTO bike_schema.mapdir SET value='$data', description='$description', routeType='$routeType', flagRoute = '$flagRoute'";
		$result = mysql_query($query);

		if (!$result) {
  			die('Invalid query: ' . mysql_error());
		}
	}// if user chooses to save data


	// if user tries to upload the last saved route
	if($_REQUEST['command']=='fetch')
	{
		//$query = "SELECT * FROM bike_schema.mapdir";
		$query = "SELECT * FROM bike_schema.mapdir WHERE ID = (SELECT MAX(ID) FROM bike_schema.mapdir WHERE flagRoute = '1')";

		if(!($res = mysql_query($query)))die(mysql_error());
        $rs = mysql_fetch_array($res,0);
        die($rs['value']);
	}// if user tries to upload the last saved route

	if($_REQUEST['command']=='viewRoutes')
	{
		$query = "SELECT * FROM bike_schema.mapdir";

        if(!($res = mysql_query($query)))
		{
			die(mysql_error());
		}

		$routes = array();
	    while ($row = mysql_fetch_array($res, MYSQL_ASSOC))
	    {
	    	array_push($routes, $row["value"]);
		}
		die($routes);

	}


?>schema
